<template>
  <div>
    <el-dialog
      :title="this.$t('common.Add')"
      :visible.sync="Visible"
      width="500px"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div class="dialogBox">
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              {{$t('CustomerManagementLang.iphone')}}:
            </div>
            <div class="contant iphoneClass">
              <el-input
                ref="mobile"
                @focus="handleFocus($event)"
                @input="handleInput"
                :placeholder="$t('common.input_plactholder')"
                v-model="data.mobile"
              >
                <el-popover style="min-width:100px;"  class="accountTitle" slot="prepend" placement="bottom" trigger="click" ref="popoverRefLang">
                  <ul style="margin: 0;height:100%;overflow:auto;cursor: pointer;">
                    <li
                      :style="{
                        backgroundColor:data.area_code == item.name? '#fff065': '#fff',
                        padding:'5px 0 5px 10px'
                      }"
                      v-for="item in PhoneAreaCodeList"
                      :key="item.id"
                      @click="handleChangePhoneAreaCode(item)"
                    >
                      <img style="width: 20px;margin-right:10px;" :src="$baseUrl+item.icon" alt="">
                      <span>{{item.code}}</span>
                    </li>
                  </ul>
                  <span style="cursor: pointer;display: flex; align-items: center;" slot="reference">
                    <img :src="$baseUrl+''+PhoneAreaIcon" alt="">
                    <div style="margin-left:5px;">{{data.area_code}}</div>
                  </span
                  >
                </el-popover>
              </el-input>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              {{$t('CustomerManagementLang.email')}}:
            </div>
            <div class="contant">
              <el-input
                ref="email"
                @focus="handleFocus($event)"
                :placeholder="$t('common.input_plactholder')"
                v-model="data.email"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              <div class="RequiredRowTip">*</div>
              {{$t('CustomerManagementLang.Name')}}:
            </div>
            <div class="contant">
              <el-input
                ref="name"
                @blur="handleBlur('name',$event)"
                @focus="handleFocus($event)"
                :placeholder="$t('common.input_plactholder')"
                v-model="data.name"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              {{$t('CustomerManagementLang.code')}}:
            </div>
            <div class="contant">
              <el-input
                :placeholder="$t('common.input_plactholder')"
                v-model="data.nickname"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              <div class="RequiredRowTip">*</div>
              {{$t('ExpressOrderManagementLang.sale')}}:
            </div>
            <div class="contant">
              <el-select
                ref="parent_id"
                filterable
                @blur="handleBlur('parent_id', $event)"
                @change="handleChang('parent_id')"
                v-model="data.parent_id"
                :placeholder="$t('common.select_plactholder')"
              >
                <el-option
                  v-for="item in options_sale"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              {{$t('CustomerManagementLang.default_destination')}}:
            </div>
            <div class="contant">
              <el-select
                filterable
                v-model="data.target_local"
                :placeholder="$t('common.select_plactholder')"
              >
                <el-option
                  v-for="item in options_target_local"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              <div class="RequiredRowTip">*</div>
              {{$t('CustomerManagementLang.password')}}:
            </div>
            <div class="contant">
              <el-input
                style="-webkit-text-security:disc"
                ref="password"
                @blur="handleBlur('password',$event)"
                @focus="handleFocus($event)"
                :placeholder="$t('common.input_plactholder')"
                v-model="data.password"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="dialogBoxItem">
          <div style="width:100%" class="dialogBoxItemRow">
            <div class="title">
              <div class="RequiredRowTip">*</div>
              {{$t('CustomerManagementLang.passwordTrue')}}:
            </div>
            <div class="contant">
              <el-input
                style="-webkit-text-security:disc"
                ref="conmfirm_password"
                @blur="handleBlur('conmfirm_password',$event)"
                @focus="handleFocus($event)"
                :placeholder="$t('common.input_plactholder')"
                v-model="data.conmfirm_password"
              ></el-input>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button  type="primary" @click="confirm" size="small">{{$t('common.confirm')}}</el-button
        >
        <el-button @click="cancel" size="small">{{$t('common.cancel')}}</el-button>
      </span>
      
    </el-dialog>
  </div>
</template>

<script>
import { validateEmail } from '@/utils/func.js'
export default {
  components: {
  },
  props: {
    // 接收绑定参数
    
  },
  data() {
    return {
      Visible: false,
      PhoneAreaIcon:'',
      PhoneAreaCodeList:[],
      data: {
        area_code:'',
        mobile:"",
        email:"",
        name:"",
        nickname:"",
        parent_id:"",
        target_local:"",
        password:"",
        conmfirm_password:"",
      },
      options_sale:[],
      options_target_local:[],
      confirmTrue:true,//确定按钮保护
    };
  },
  created() {
   
  },
  mounted() {},
  watch: {},
  methods: {
    init(data) {
      this.getPhoneAreaCodeList()
      this.getOptions()
      this.Visible = true;
    },
    //获取下拉单数据
    getOptions(){
      this.$get("getMemberOptionalAddress").then((res) => {
        this.options_target_local = res.data;
      });
    },
    getPhoneAreaCodeList(){
      //获取区号列表
      this.$get("getPhoneAreaCode").then((res) => {
        if(res.success){
          this.PhoneAreaCodeList = res.data;
          if(res.data.length>0){
            this.PhoneAreaIcon=res.data[0].icon
            this.data.area_code=res.data[0].code
          }
        }
      });
      this.$get("getMemberBindableSales").then((res) => {
        if(res.success){
          let current_user_data = JSON.parse(sessionStorage.getItem("userData"))
          this.options_sale = res.data.filter(item=>{return item.id!==current_user_data.id})
          this.options_sale.unshift(current_user_data)
          this.data.parent_id=current_user_data.id
        }
      });
    },
    handleChangePhoneAreaCode(item){
      this.PhoneAreaIcon=item.icon
      this.data.area_code=item.code
    },
    // 提交
    confirm() {
      //是否有空值必选项标识
      let type = true;
      //输入框判断是否有值
      if(!!this.data.email){
        if(!validateEmail(this.data.email)){
          this.$message.warning(this.$t('AccountLang.message_email'));
          this.$refs.email.$el.childNodes[1].style.border='1px solid red'
          type=false
          return
        }
      }
      if(!this.data.mobile&&!this.data.email){
        this.$message.warning(this.$t('AccountLang.message'));
        this.$refs.mobile.$el.childNodes[1].style.border='1px solid red'
        this.$refs.email.$el.childNodes[1].style.border='1px solid red'
      }
      if(!this.data.name){
        this.$refs.name.$el.childNodes[1].style.border='1px solid red'
        type=false
      }
      if(!this.data.password){
        this.$refs.password.$el.childNodes[1].style.border='1px solid red'
        type=false
      }
      if(!this.data.conmfirm_password){
        this.$refs.conmfirm_password.$el.childNodes[1].style.border='1px solid red'
        type=false
      }else{
        if(this.data.password!==this.data.conmfirm_password){
          this.$message.warning(this.$t('CustomerManagementLang.password_msg'));
          this.$refs.conmfirm_password.$el.childNodes[1].style.border='1px solid red'
          type=false
          return
        }
      }
      if (type && this.confirmTrue) {
        this.confirmTrue=false
        window.setTimeout(()=>{
          this.confirmTrue=true
        },1000);
        let params = new FormData();
        params.append("area_code", this.data.area_code);
        params.append("mobile", this.data.mobile);
        params.append("email", this.data.email);
        params.append("name", this.data.name);
        params.append("nickname", this.data.nickname);
        params.append("parent_id", this.data.parent_id);
        params.append("target_local", this.data.target_local);
        params.append("password", this.data.password);
        this.$postForm("registerAccount", params).then((res) => {
          this.confirmTrue = true;
          if(res.success){
            this.$message.success(res.msg);
            this.$emit("success");
            this.cancel()
          }
        });
      }
    },
    //取消
    cancel() {
      this.data = {
        area_code:'',
        mobile:"",
        email:"",
        name:"",
        nickname:"",
        parent_id:"",
        target_local:"",
        password:"",
        conmfirm_password:"",
      };
      this.Visible = false;
      
      let dom = document.getElementsByClassName("el-input__inner");
      for (let i = 0; i < dom.length; i++) {
         dom[i].style.border = "1px solid #E0E3E9";
      }
    },
    //输入框、下拉框失去焦点边框变色
    handleBlur(name, event) {
      if (!this.data[name]&&this.data[name]!==0) {
          event.target.style.border = "1px solid red";
        }
    },
    //输入框、下拉框获取焦点边框变色
    handleFocus(event) {
      event.target.style.border = "1px solid #E0E3E9";
    },
    handleInput(value) {
      // 只允许输入数字
      this.data.mobile = value.replace(/[^\d]/g, '');
    },
    //下拉框有值后边框变色
    handleChang(data) {
      if (this.data[data].length==0) {
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border = "1px solid red";
      }else{
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border ="1px solid #E0E3E9 ";
      }
    },
  },
};
</script>

<style scoped lang="less" >
  .dialogBox .dialogBoxItem .dialogBoxItemRow{
    width: 23%;
  }
  /deep/ .el-dialog__wrapper{
    .el-dialog{
      min-width: 500px;
    }
  }
  .iphoneClass{
    position: relative;
    /deep/.el-input__inner{
      padding-left: 105px;
    }
    /deep/.el-input-group__prepend{
      position: absolute;
      width: 100px;
      top: 1px;
      left: 2px;
      border: 0;
      height: 32px;
      padding: 0;
    }
  }
  .accountTitle{
    display: flex;
    align-items: center;
    justify-content: center;
    /deep/.el-popover__reference{
      height: 34px;
    }
    img{
      width: 25px;
      height: 25px;
      border-radius: 50%;
    }
  }
</style>